﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Modles.DTO;
using Modles.Models;

namespace BilbilDAL
{
    public class CommentsDAL
    {
        public int GetUserCommentsCount(int UserInfoId)
        {
            using (VideoPlatformDbContext con = new VideoPlatformDbContext())
            {
                return con.Comments.Where(x => x.UserInfoId == UserInfoId).Count();
            }
        }


        /// <summary>
        /// 获取评论列表
        /// </summary>
        /// <param name="videoInfoId"></param>
        /// <returns></returns>
        public List<VideoComment_DTO> GetVideoComments(int videoInfoId)
        {
            using (VideoPlatformDbContext com = new VideoPlatformDbContext())
            {
                IQueryable<VideoComment_DTO> queryable = from a in com.Comments.Where(x => x.VideoInfoId == videoInfoId)
                                                         join b in com.UserInfos on a.UserInfoId equals b.Id
                                                         select new VideoComment_DTO()
                                                         {
                                                             Avatar = b.Avatar,
                                                             Name = b.Name,
                                                             Id = a.Id,
                                                             VideoInfoId = a.VideoInfoId,
                                                             UserInfoId = a.UserInfoId,
                                                             Content = a.Content,
                                                             Time = a.Time,
                                                             Like = a.Like,
                                                             NegativeReviews = a.NegativeReviews,
                                                             LapTop = a.LapTop
                                                         };
                return queryable.ToList();
            }
        }
        /// <summary>
        /// 发布视频评论
        /// </summary>
        /// <param name="userInfoId"></param>
        /// <param name="videoInfoId"></param>
        /// <param name="Content"></param>
        /// <returns></returns>
        public bool AddVideoComment(int userInfoId, int videoInfoId, string content)
        {
            using (VideoPlatformDbContext con = new VideoPlatformDbContext())
            {
                if(!con.VideoInfos.Any(x => x.Id == videoInfoId)) { return false; }
                if(content.Trim() == null) { return false; }
                con.Comments.Add(new Comment()
                {
                    VideoInfoId = videoInfoId,
                    UserInfoId = userInfoId,
                    Content = content,
                    Time = DateTime.Now,
                    Like = 0,
                    NegativeReviews = 0,
                    LapTop = 0
                });
                return con.SaveChanges() > 0;
            }
        }
    }
}
